package com.weihui.jxc.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.weihui.jxc.common.Constants;
import com.weihui.jxc.common.ErrorManager;
import com.weihui.jxc.model.Operator;

/**
 * 管理员类Dao (针对 t_operator表进行CRUD)
 * 
 * @author xiaowei
 *
 */
public class OperatorDao implements BaseDao<Operator> {
	SqlManager manager = null;

	public OperatorDao() {
		super();
		manager = SqlManager.createInstance();
		manager.connectDB();// 获取数据库连接
	}

	/**
	 * 查询用户名和密码是否正确
	 * 
	 * @param operator
	 * @return
	 */
	public boolean loginCheck(Operator operator) {
		boolean result = false;
		try {
			// 编写sql语句
			String sql = "select * from t_operator where username =? and password=?";
			// 封装参数
			Object[] params = new Object[] { operator.getUsername(), operator.getPassword() };
			// 获取结果集
			ResultSet rs = manager.executeQuery(sql, params, Constants.PSTM_TYPE);
			while (rs.next()) {
				result = true;
			}
			manager.closeDB();
		} catch (SQLException e) {
			ErrorManager.printError("OperatorDao loginCheck()", e);
		}
		return result;
	}

	/**
	 * 通过登录名查询操作者信息
	 * 
	 * @param username：登录名
	 * @return：返回的就是操作者对象
	 */
	public Operator getOperator(String username) {
		// 编写sql
		String sql = "select * from t_operator where username=?";
		// 封装参数
		Object[] params = new Object[] { username };
		// 获取结果集
		ResultSet rs = manager.executeQuery(sql, params, Constants.PSTM_TYPE);
		// 解析数据
		Operator operator = null;
		try {
			if (rs != null) {
				while (rs.next()) {
					operator = new Operator(rs.getString("username"), rs.getString("password"), rs.getString("name"),
							rs.getString("power"));
				}
			}
		} catch (SQLException e) {
			ErrorManager.printError("OperatorDao getOperator()", e);
		}
		return operator;
	}

	/**
	 * 添加操作员
	 */
	@Override
	public boolean save(Operator o) {
		String sql = "insert into t_operator values(?,?,?,?)";
		Object[] params = new Object[] { o.getUsername(), o.getPassword(), o.getName(), o.getPower() };
		return manager.executeUpdate(sql, params, Constants.PSTM_TYPE);
	}

	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 查询操作者
	 */
	@Override
	public List<Operator> query(Operator t) {
		List<Operator> list = null;
		Object[] params = null;
		String sql = null;
		if (t == null) {
			sql = "select * from t_operator";
		}
		ResultSet rs = manager.executeQuery(sql, params, Constants.PSTM_TYPE);
		if (rs != null) {
			list = new ArrayList<Operator>();
			try {
				while (rs.next()) {
					Operator o = new Operator();
					o.setUsername(rs.getString("username"));
					o.setPassword(rs.getString("password"));
					o.setName(rs.getString("name"));
					o.setPower(rs.getString("power"));
					list.add(o);
				}
			} catch (SQLException e) {
				ErrorManager.printError("OperatorDao query()", e);
			}
		}
		return list;
	}

	/**
	 * 修改操作员
	 */
	@Override
	public boolean update(Operator t) {
		String sql = null;
		Object[] params = null;
		if ((t.getUsername() != null && t.getUsername().length() > 0)
				&& (t.getPassword() != null && t.getPassword().length() > 0)) {
			sql = "update t_operator set password=? where username=?";
			params = new Object[] { t.getPassword(), t.getUsername() };
		} else {
			sql = "update t_operator set name=?,power=? where username=?";
			params = new Object[] { t.getName(), t.getPower(), t.getUsername() };
		}

		return manager.executeUpdate(sql, params, Constants.PSTM_TYPE);
	}

	/**
	 * 删除操作员
	 * 
	 * @param username
	 * @return
	 */
	public boolean delete(String username) {
		String sql = "delete from t_operator where username=?";
		Object[] params = new Object[] { username };
		return manager.executeUpdate(sql, params, Constants.PSTM_TYPE);
	}

	@Override
	public boolean update(String id, Integer available) {
		// TODO Auto-generated method stub
		return false;
	}
}